<?php
include('../../include/functions.inc.php');
include('../../include/report.inc.php');

$arrData = array();
$strQuery ="SELECT  cllg_college_cycle.code, 
                    SUM( cllg_factura.total ) total
            FROM    cllg_college_cycle
                    LEFT JOIN cllg_factura 
                        ON cllg_factura.fecha >= cllg_college_cycle.date_begin
                        AND cllg_factura.fecha <= cllg_college_cycle.date_end
                        AND cllg_factura.status = 1
            WHERE   cllg_college_cycle.fk_entity = {$user->entity}
            GROUP BY cllg_college_cycle.code";
$db->query($strQuery);
while( $arrTMP = $db->fetch_assoc() ) {
    $arrData[$arrTMP['code']] = floatval($arrTMP['total']);
}
$db->free_result($db->query_id);

while( $arrU = each($arrData) ) {
    $strQuery ="UPDATE  cllg_college_cycle 
                SET     money_in = " . $arrU['value'] . "
                WHERE   code = {$arrU['key']}";
    $db->query($strQuery);
}

$report = new Report;
$report->tabla = "cllg_college_cycle";
$report->campos = array("CODE" => "CICLO", "DATE_BEGIN" => "INICIA",
    "DATE_END" => "FINALIZA",
    "MONEY_IN" => "TOTAL INGRESO",
    "MONEY_OUT" => "TOTAL EGRESO",
    "ACTIVE_STUDENTS" => "TOTAL INSCRITOS",
    "STATUS" => "ESTADO(1=act./0=inact.)");

$campo = new FiltroCampo;
$campo->nombre = "FK_ENTITY";
$campo->defValue = $user->entity;
$report->filtroFijo[] = $campo;

$report->titulo = "Reporte Ciclos de Estudio";
$report->requireFilter = false;
$report->preLoaded = true;
$_SESSION[$report->sessionName] = serialize($report);
$report->ready();
?>